﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;

namespace FinWeb3.Models.Repository
{
    public class BankRepository : IRepository<Bank>
    {
        public void Save(Bank item, IIdentity user)
        {
            if (item.Id == 0)
            {
                item.UserName = user.Name;
                item.Save();
            }
            else
            {
                var bank = Get(item.Id, user);

                if (bank != null)
                {
                    bank.AccountId = item.AccountId;
                    bank.BankId = item.BankId;
                    bank.Limit = item.Limit;
                    bank.Name = item.Name;
                    bank.UserName = user.Name;

                    bank.Save();
                }
            }
        }

        public void Delete(Bank item, IIdentity user)
        {
            var bank = Get(item.Id, user);

            if (bank != null)
                bank.Delete();
        }

        public Bank Get(int id, IIdentity user)
        {
            return Bank.All().Where(x => x.UserName == user.Name && x.Id == id).FirstOrDefault();
        }

        public IQueryable<Bank> GetAll(IIdentity user)
        {
            return Bank.All().Where(x => x.UserName == user.Name);
        }
    }
}
